package servlet;

import domain.Comments;
import domain.News;
import domain.Topic;
import services.CommentsServicesImpl;
import services.NewsServicesImpl;
import services.TopicServicesImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 新闻请求:URL:/news?method=方法
 * 首页:URL:/news?method=getIndex
 * 添加新闻:URL:/news?method=addNews?新闻实体类参数
 */
public class NewsServlet extends HttpServlet {
    TopicServicesImpl topicServices = new TopicServicesImpl();
    NewsServicesImpl newsServices = new NewsServicesImpl();
    CommentsServicesImpl commentsServices = new CommentsServicesImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取方法名
        String method = request.getParameter("method");
        //获取session
        HttpSession session = request.getSession();
        //获取国内、国际、娱乐新闻
        List<News> chinaNews = newsServices.getNewsByTopic(1);
        List<News> internationalNews = newsServices.getNewsByTopic(2);
        List<News> entertainmentNews = newsServices.getNewsByTopic(5);
        //将国内、国际、娱乐新闻存入session
        session.setAttribute("chinaNews", chinaNews);
        session.setAttribute("internationalNews", internationalNews);
        session.setAttribute("entertainmentNews", entertainmentNews);
        if (method.equals("getIndexByTopic")) {//通过新闻分类显示新闻列表
            //获取所有topic
            List<Topic> topicList = topicServices.getAllTopic();
            session.setAttribute("allTopic", topicList);
            //获取页码、每页条数、获取具体新闻类型
            String pageNo = request.getParameter("pageNo");
            String size = request.getParameter("size");
            String tid = request.getParameter("tid");
            //获取该类型的新闻有多少条，计算需要多少页来展示
            int countNews = newsServices.countNews(Integer.parseInt(tid));
            int totalPages = (int) Math.ceil(countNews / Double.parseDouble(size));
            //将获取到的页码、每页条数、获取具体新闻类型存入request域
            request.setAttribute("pageNo", Integer.parseInt(pageNo));
            request.setAttribute("totalPages", totalPages);
            request.setAttribute("tid", Integer.parseInt(tid));
            //获取具体类型的新闻对象并存入request域
            List<News> newsList = newsServices.getNewsByNumber(Integer.parseInt(tid), Integer.parseInt(pageNo), Integer.parseInt(size));
            request.setAttribute("newsList", newsList);
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        } else if (method.equals("readNews")) {//新闻阅读页面
            //获取新闻的nid，通过nid查询新闻的全部信息，封装News对象
            String nid = request.getParameter("nid");
            News newsByNid = newsServices.getNewsDetailsByNid(Integer.parseInt(nid));
            //将查询到的新闻存入session
            request.setAttribute("news", newsByNid);
            //获取评论
            //1.进入服务层获取评论对象
            List<Comments> commentsByCnid = commentsServices.getCommentsByCnid(Integer.parseInt(nid));
            //2.将评论对象存入request域
            request.setAttribute("comments", commentsByCnid);
            //跳转到新闻阅读页面
            request.getRequestDispatcher("/newspages/news_read.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
